package org.example.warehouse.controller;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import org.example.warehouse.domain.SellStoreDetail;
import org.example.warehouse.service.ISellStoreDetailService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 仓库出库详情Controller
 *
 * @author 小孟微信：xiaomengcode
 * @date 2024-04-12
 */
@Controller
@RequestMapping("/warehouse/sellStoreDetail")
public class SellStoreDetailController extends BaseController
{
    private String prefix = "warehouse/sellStoreDetail";

    @Autowired
    private ISellStoreDetailService sellStoreDetailService;

    @GetMapping()
    public String sellStoreDetail(SellStoreDetail sellStoreDetail,ModelMap mmp)
    {
        mmp.put("sellStoreDetail",sellStoreDetail);
        return prefix + "/sellStoreDetail";
    }

    /**
     * 查询仓库出库详情列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SellStoreDetail sellStoreDetail)
    {
        startPage();
        List<SellStoreDetail> list = sellStoreDetailService.selectSellStoreDetailList(sellStoreDetail);
        return getDataTable(list);
    }

    /**
     * 导出仓库出库详情列表
     */
    @Log(title = "仓库出库详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SellStoreDetail sellStoreDetail)
    {
        List<SellStoreDetail> list = sellStoreDetailService.selectSellStoreDetailList(sellStoreDetail);
        ExcelUtil<SellStoreDetail> util = new ExcelUtil<SellStoreDetail>(SellStoreDetail.class);
        return util.exportExcel(list, "仓库出库详情数据");
    }

    /**
     * 新增仓库出库详情
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存仓库出库详情
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SellStoreDetail sellStoreDetail)
    {
        sellStoreDetail.getParams().put("code",getCurrentOrderNo(sellStoreDetail));
        return toAjax(sellStoreDetailService.insertSellStoreDetail(sellStoreDetail));
    }

    private Long getCurrentOrderNo(SellStoreDetail sellStoreDetail) {
        sellStoreDetail.setCreateTime(DateUtils.getNowDate());
        startPage();
        List<SellStoreDetail> list = sellStoreDetailService.selectSellStoreDetailList(sellStoreDetail);
        TableDataInfo dataTable = getDataTable(list);
        Long total =dataTable.getTotal();
        return total;
    }
    /**
     * 修改仓库出库详情
     */
    @GetMapping("/edit/{sellDetailId}")
    public String edit(@PathVariable("sellDetailId") Long sellDetailId, ModelMap mmap)
    {
        SellStoreDetail sellStoreDetail = sellStoreDetailService.selectSellStoreDetailBySellDetailId(sellDetailId);
        mmap.put("sellStoreDetail", sellStoreDetail);
        return prefix + "/edit";
    }

    /**
     * 修改保存仓库出库详情
     */
    @Log(title = "仓库出库详情", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SellStoreDetail sellStoreDetail)
    {
        return toAjax(sellStoreDetailService.updateSellStoreDetail(sellStoreDetail));
    }

    /**
     * 删除仓库出库详情
     */
    @Log(title = "仓库出库详情", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sellStoreDetailService.deleteSellStoreDetailBySellDetailIds(ids));
    }
}
